/*
 *   项目名称：rpc-demo
 *   文件名称：com.xiaolon.rpc.loadbalancer.RoundRobinLoadBalancer
 *
 *   创建人：  ZHOU XIAO LONG
 *   创建日期：2024/12/11
 *
 */
package com.xiaolon.rpc.loadbalancer;

import com.xiaolon.rpc.model.ServiceMetaInfo;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/*
 * 类描述：RoundRobinLoadBalancer
 * 轮询负载均衡器
 */
public class RoundRobinLoadBalancer implements LoadBalancer{

    /**
     * 当前轮询的下标
     */
    private final AtomicInteger currentIndex = new AtomicInteger(0);

    @Override
    public ServiceMetaInfo select(Map<String, Object> requestParams, List<ServiceMetaInfo> serviceMetaInfoList) {
        if(serviceMetaInfoList.isEmpty()){
            return null;
        }

        // 只有一个服务，无需轮询
        int size = serviceMetaInfoList.size();
        if(size == 1){
            return serviceMetaInfoList.get(0);
        }

        // 去模轮询
        int index = currentIndex.getAndIncrement() % size;
        return serviceMetaInfoList.get(index);
    }
}
